<?php namespace App\Admin\Access\User\Listeners;

use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Service\Listeners\CreateListener as Base;
use App\Admin\Access\User\Repository as UserRepository;

class CreateListener extends Base {
    
    public function onCreateGeneratedData(){
        $data = $this->service->createData;
        if(!$this->checkUsername($data['username'])){
            echo $this->service->inputError('用户名已存在');
            exit();
        }
        //密码处理
        $data['saltcode'] = rand(10000, 99999);
        $data['password'] = md5($data['password'].$data['saltcode']);
        //登录信息初始化
        $data['login_error_count'] = '0';
        $data['islocked'] = '0';
        $this->service->createData = $data;
    }
    
    private function checkUsername($username){
        return !with(new UserRepository())->addCriteria(new FixedCriteria('username', $username))->count() > 0;
    }
}